package com.lvmama.rhino.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.apache.commons.io.FileUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HttpsUtil {
    private static final Logger LOGGER = LoggerFactory.getLogger(HttpsUtil.class);
    private static final String CHARACTER_ENCODING = "UTF-8";
    private static final String CONTENT_TYPE = "Content-Type";
    public static final String HTTP_ERROR_PREFIX = "passport";
    private static final int CONNECTION_TIMEOUT = 10000;
    private static final int SO_TIMEOUT = 180000;


    /**
     * HttpResponse包装类
     */
    public static class HttpResponseWrapper {
        private HttpResponse httpResponse;
        private final HttpClient httpClient;

        public HttpResponseWrapper(HttpClient httpClient, HttpResponse httpResponse) {
            this.httpClient = httpClient;
            this.httpResponse = httpResponse;
        }

        public HttpResponseWrapper(HttpClient httpClient) {
            this.httpClient = httpClient;
        }

        public HttpResponse getHttpResponse() {
            return httpResponse;
        }

        public void setHttpResponse(HttpResponse httpResponse) {
            this.httpResponse = httpResponse;
        }

        /**
         * 获得流类型的响应
         */
        public InputStream getResponseStream() throws IllegalStateException, IOException {
            return httpResponse.getEntity().getContent();
        }

        /**
         * 获得字符串类型的响应
         */
        public String getResponseString(String responseCharacter) throws ParseException, IOException {
            HttpEntity entity = getEntity();
            String responseStr = EntityUtils.toString(entity, responseCharacter);
            if (entity.getContentType() == null) {
                responseStr = new String(responseStr.getBytes("iso-8859-1"), responseCharacter);
            }
            EntityUtils.consume(entity);
            return responseStr;
        }

        public String getResponseString() throws ParseException, IOException {
            return getResponseString(CHARACTER_ENCODING);
        }

        /**
         * 获得响应状态码
         */
        public int getStatusCode() {
            return httpResponse.getStatusLine().getStatusCode();
        }

        /**
         * 获得响应状态码并释放资源
         */
        public int getStatusCodeAndClose() {
            close();
            return getStatusCode();
        }

        public HttpEntity getEntity() {
            return httpResponse.getEntity();
        }

        /**
         * 释放资源
         */
        public void close() {
            httpClient.getConnectionManager().shutdown();
        }
    }

    /**
     * POST方式提交表单数据，返回响应对象
     *
     * @throws Exception
     */
    private static HttpResponseWrapper requestPostFormResponse(String url, Map<String, String> requestParas, String requestCharacter, int connectionTimeout, int soTimeout) throws Exception {
        HttpClient client = null;
        if (url.startsWith("https")) {
            client = createHttpsClient(connectionTimeout, soTimeout);
        } else {
            client = createHttpClient(connectionTimeout, soTimeout);
        }
        HttpPost httpPost = new HttpPost(url);
        List<NameValuePair> formParams = initNameValuePair(requestParas);
        httpPost.setEntity(new UrlEncodedFormEntity(formParams, requestCharacter));
        HttpResponse httpResponse = client.execute(httpPost); //执行POST请求
        return new HttpResponseWrapper(client, httpResponse);
    }

    /**
     * POST方式提交表单数据，不会自动重定向
     */
    public static String requestPostForm(String url, Map<String, String> requestParas, String requestCharacter, String responseCharacter, int connectionTimeout, int soTimeout) {
        HttpResponseWrapper httpResponseWrapper = null;
        String responseStr = "";
        try {
            httpResponseWrapper = requestPostFormResponse(url, requestParas, requestCharacter, connectionTimeout, soTimeout);
            responseStr = httpResponseWrapper.getResponseString(responseCharacter);
        } catch (Exception e) {
            responseStr = HTTP_ERROR_PREFIX + e.toString();
            LOGGER.error("requestPostForm request Exception:", e);
        } finally {
            if (httpResponseWrapper != null) {
                httpResponseWrapper.close();
            }
        }
        return responseStr;
    }
    /**
     * POST方式提交表单数据，不会自动重定向
     */
    public static String requestPostForm(String url) {
        return requestPostForm(url, null, CHARACTER_ENCODING, CHARACTER_ENCODING, CONNECTION_TIMEOUT, SO_TIMEOUT);
    }
    
    /**
     * POST方式提交表单数据，不会自动重定向
     */
    public static String requestPostForm(String url, Map<String, String> requestParas) {
        return requestPostForm(url, requestParas, CHARACTER_ENCODING, CHARACTER_ENCODING, CONNECTION_TIMEOUT, SO_TIMEOUT);
    }


    /**
     * POST方式提交Json数据，返回响应对象
     *
     * @throws Exception
     */
    private static HttpResponseWrapper requestPostJsonResponse(String url, String json, String requestCharacter, int connectionTimeout, int soTimeout) throws Exception {
        HttpClient client = null;
        if (url.startsWith("https")) {
            client = createHttpsClient(connectionTimeout, soTimeout);
        } else {
            client = createHttpClient(connectionTimeout, soTimeout);
        }

        String contentType = "application/json; charset=" + requestCharacter;
        HttpPost httpPost = new HttpPost(url);
        httpPost.setHeader(CONTENT_TYPE, contentType);
        StringEntity se = new StringEntity(json, requestCharacter);
        httpPost.setEntity(se);
        int retryTimes = 3;
        HttpResponse httpResponse = null; //执行POST请求

        while (retryTimes > 0) {
            try {
                httpResponse = client.execute(httpPost);
                break;
            } catch (IOException e) {
                if (retryTimes <= 1) {
                    throw e;
                }
            } //执行POST请求
            retryTimes--;
        }
        return new HttpResponseWrapper(client, httpResponse);
    }

    /**
     * POST方式提交Json数据
     */
    public static String requestPostJson(String url, String json, String requestCharacter, String responseCharacter, int connectionTimeout, int soTimeout) {
        HttpResponseWrapper httpResponseWrapper = null;
        String responseStr = "";
        try {
            httpResponseWrapper = requestPostJsonResponse(url, json, requestCharacter, connectionTimeout, soTimeout);
            responseStr = httpResponseWrapper.getResponseString(responseCharacter);
        } catch (Exception e) {
            responseStr = HTTP_ERROR_PREFIX + e.toString();
            LOGGER.error("requestPostForm request Exception:", e);
        } finally {
            if (httpResponseWrapper != null) {
                httpResponseWrapper.close();
            }
        }
        return responseStr;
    }

    /**
     * POST方式提交JSON数据
     */
    public static String requestPostJson(String url, String json) {
        return requestPostJson(url, json, CHARACTER_ENCODING, CHARACTER_ENCODING, CONNECTION_TIMEOUT, SO_TIMEOUT);
    }

    private static HttpResponseWrapper requestGetResponse(String url, int connectionTimeout, int soTimeout) throws Exception {
        HttpClient client = null;
        if (url.startsWith("https")) {
            client = createHttpsClient(connectionTimeout, soTimeout);
        } else {
            client = createHttpClient(connectionTimeout, soTimeout);
        }
        HttpGet httpGet = new HttpGet(url);
        HttpResponse httpResponse = client.execute(httpGet);
        return new HttpResponseWrapper(client, httpResponse);

    }

    /**
     * GET方式提交URL请求，会自动重定向
     */
    public static String requestGet(String url, String responseCharacter, int connectionTimeout, int soTimeout) {
        HttpResponseWrapper httpResponseWrapper = null;
        String responseStr = "";
        try {
            httpResponseWrapper = requestGetResponse(url, connectionTimeout, soTimeout);
            responseStr = httpResponseWrapper.getResponseString(responseCharacter);
        } catch (Exception e) {
            responseStr = HTTP_ERROR_PREFIX + e.toString();
            LOGGER.error("requstGet request Exception:", e);
        } finally {
            if (httpResponseWrapper != null) {
                httpResponseWrapper.close();
            }
        }
        return responseStr;
    }

    /**
     * GET方式提交URL请求，会自动重定向
     */
    public static String requestGet(String url) {
        return requestGet(url, CHARACTER_ENCODING, CONNECTION_TIMEOUT, SO_TIMEOUT);
    }

    /**
     * POST方式提交非表单数据，返回响应对象
     *
     * @throws Exception
     */
    private static HttpResponseWrapper requestPostData(String url, String data, String contentType, String requestCharacter, int connectionTimeout, int soTimeout) throws Exception {
        HttpClient client = null;
        if (url.startsWith("https")) {
            client = createHttpsClient(connectionTimeout, soTimeout);
        } else {
            client = createHttpClient(connectionTimeout, soTimeout);
        }
        HttpPost httpPost = new HttpPost(url);
        httpPost.setHeader(CONTENT_TYPE, contentType);
        httpPost.setEntity(new StringEntity(data, requestCharacter));
        HttpResponse httpResponse = client.execute(httpPost);
        return new HttpResponseWrapper(client, httpResponse);
    }

    /**
     * POST方式提交非表单数据，返回响应对象
     * 不校验host
     *
     * @throws Exception
     */
    private static HttpResponseWrapper requestPostDataNotValidateHost(String url, String data, String contentType, String requestCharacter, int connectionTimeout, int soTimeout) throws Exception {
        HttpClient client = null;
        if (url.startsWith("https")) {
            client = createHttpsClientNotValidateHost(connectionTimeout, soTimeout);
        } else {
            client = createHttpClient(connectionTimeout, soTimeout);
        }
        HttpPost httpPost = new HttpPost(url);
        httpPost.setHeader(CONTENT_TYPE, contentType);
        httpPost.setEntity(new StringEntity(data, requestCharacter));
        HttpResponse httpResponse = client.execute(httpPost);
        return new HttpResponseWrapper(client, httpResponse);
    }

    /**
     * POST方式提交json String，返回String
     *
     * @throws Exception
     */
    public static String sendPost(String url, String param) {
        PrintWriter out = null;
        BufferedReader in = null;
        StringBuffer result = new StringBuffer();
        try {
            URL realUrl = new URL(url);
            // 打开和URL之间的连接
            URLConnection conn = realUrl.openConnection();
            // 设置通用的请求属性
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent",
                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            // 发送POST请求必须设置如下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);
            // 获取URLConnection对象对应的输出流
            out = new PrintWriter(conn.getOutputStream());
            // 发送请求参数
            out.print(param);
            // flush输出流的缓冲
            out.flush();
            // 定义BufferedReader输入流来读取URL的响应
            in = new BufferedReader(
                    new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result.append(line);
            }
        } catch (Exception e) {
            LOGGER.error("发送 POST 请求出现异常！", e);
        }
        //使用finally块来关闭输出流、输入流
        finally {
            try {
                if (out != null) {
                    out.close();
                }
                if (in != null) {
                    in.close();
                }
            } catch (IOException ex) {
                LOGGER.error("关闭流出现异常！", ex);
            }
        }
        return result.toString();
    }


    /**
     * POST方式提交非表单数据，返回响应对象
     *
     * @throws Exception
     */
    private static HttpResponseWrapper requestPostData(String url, String data, String contentType, String requestCharacter) throws Exception {
        return requestPostData(url, data, contentType, requestCharacter, CONNECTION_TIMEOUT, SO_TIMEOUT);
    }

    /**
     * POST方式提交非表单数据，返回响应对象
     * 不校验host
     *
     * @throws Exception
     */
    private static HttpResponseWrapper requestPostDataNotValdateHost(String url, String data, String contentType, String requestCharacter) throws Exception {
        return requestPostDataNotValidateHost(url, data, contentType, requestCharacter, CONNECTION_TIMEOUT, SO_TIMEOUT);
    }

    /**
     * POST非表单方式提交XML数据
     */
    public static String requestPostXml(String url, String xmlData, String requestCharacter, String responseCharacter) {
        HttpResponseWrapper httpResponseWrapper = null;
        String responseStr = "";
        try {
            String contentType = "text/xml; charset=" + requestCharacter;
            httpResponseWrapper = requestPostData(url, xmlData, contentType, requestCharacter);
            responseStr = httpResponseWrapper.getResponseString(responseCharacter);
        } catch (Exception e) {
            responseStr = HTTP_ERROR_PREFIX + e.toString();
            LOGGER.error("requestPostXml request Exception:", e);
        } finally {
            if (httpResponseWrapper != null) {
                httpResponseWrapper.close();
            }
        }
        return responseStr;
    }

    /**
     * POST非表单方式提交XML数据 格式
     */
    public static String requestPostXmlNew(String url, String xmlData, String requestCharacter, String responseCharacter) {
        HttpResponseWrapper httpResponseWrapper = null;
        String responseStr = "";
        try {
            String contentType = "application/x-www-form-urlencoded; charset=" + responseCharacter;
            httpResponseWrapper = requestPostData(url, xmlData, contentType, requestCharacter);
            responseStr = httpResponseWrapper.getResponseString(responseCharacter);
        } catch (Exception e) {
            responseStr = HTTP_ERROR_PREFIX + e.toString();
            LOGGER.error("requestPostXml request Exception:", e);
        } finally {
            if (httpResponseWrapper != null) {
                httpResponseWrapper.close();
            }
        }
        return responseStr;
    }

    /**
     * POST非表单方式提交XML数据 格式
     */
    public static String requestPostXmlNewNotValidateHost(String url, String xmlData, String requestCharacter, String responseCharacter) {
        HttpResponseWrapper httpResponseWrapper = null;
        String responseStr = "";
        try {
            String contentType = "application/x-www-form-urlencoded; charset=" + responseCharacter;
            httpResponseWrapper = requestPostDataNotValdateHost(url, xmlData, contentType, requestCharacter);
            responseStr = httpResponseWrapper.getResponseString(responseCharacter);
        } catch (Exception e) {
            responseStr = HTTP_ERROR_PREFIX + e.toString();
            LOGGER.error("requestPostXml request Exception:", e);
        } finally {
            if (httpResponseWrapper != null) {
                httpResponseWrapper.close();
            }
        }
        return responseStr;
    }

    /**
     * POST非表单方式提交XML数据
     */
    public static String requestPostXml(String url, String xmlData) {
        return requestPostXml(url, xmlData, CHARACTER_ENCODING, CHARACTER_ENCODING);
    }

    /**
     * @param url
     * @param xmlData
     * @return
     */
    public static String requestPostXmlNew(String url, String xmlData) {
        return requestPostXmlNew(url, xmlData, CHARACTER_ENCODING, CHARACTER_ENCODING);
    }

    private static HttpClient createHttpClient(int connectionTimeout, int soTimeout) {
        HttpClient httpClient = new DefaultHttpClient();
        HttpParams params = httpClient.getParams();
        HttpConnectionParams.setConnectionTimeout(params, connectionTimeout);
        HttpConnectionParams.setSoTimeout(params, soTimeout);
        return httpClient;
    }

    private static HttpClient createHttpsClient(int connectionTimeout, int soTimeout) throws Exception {
        HttpClient httpClient = new DefaultHttpClient(); //创建默认的httpClient实例
        HttpParams params = httpClient.getParams();
        HttpConnectionParams.setConnectionTimeout(params, connectionTimeout);
        HttpConnectionParams.setSoTimeout(params, soTimeout);
        //TLS1.0与SSL3.0基本上没有太大的差别，可粗略理解为TLS是SSL的继承者，但它们使用的是相同的SSLContext
        SSLContext ctx = SSLContext.getInstance("TLS");
        //使用TrustManager来初始化该上下文，TrustManager只是被SSL的Socket所使用
        ctx.init(null, new TrustManager[]{new TrustAnyTrustManager()}, null);
        //创建SSLSocketFactory
        SSLSocketFactory socketFactory = new SSLSocketFactory(ctx);
        //通过SchemeRegistry将SSLSocketFactory注册到我们的HttpClient上
        httpClient.getConnectionManager().getSchemeRegistry().register(new Scheme("https", 443, socketFactory));
        return httpClient;
    }

    private static HttpClient createHttpsClientNotValidateHost(int connectionTimeout, int soTimeout) throws Exception {
        HttpClient httpClient = new DefaultHttpClient(); //创建默认的httpClient实例
        HttpParams params = httpClient.getParams();
        HttpConnectionParams.setConnectionTimeout(params, connectionTimeout);
        HttpConnectionParams.setSoTimeout(params, soTimeout);
        //TLS1.0与SSL3.0基本上没有太大的差别，可粗略理解为TLS是SSL的继承者，但它们使用的是相同的SSLContext
        SSLContext ctx = SSLContext.getInstance("TLS");
        //使用TrustManager来初始化该上下文，TrustManager只是被SSL的Socket所使用
        ctx.init(null, new TrustManager[]{new TrustAnyTrustManager()}, null);
        //创建SSLSocketFactory
        SSLSocketFactory socketFactory = new SSLSocketFactory(ctx);
        socketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        //通过SchemeRegistry将SSLSocketFactory注册到我们的HttpClient上
        httpClient.getConnectionManager().getSchemeRegistry().register(new Scheme("https", 443, socketFactory));
        return httpClient;
    }

    private static List<NameValuePair> initNameValuePair(Map<String, String> params) {
        List<NameValuePair> formParams = new ArrayList<NameValuePair>();
        if (params != null && params.size() > 0) {
            // 对key进行排序
            List<String> keys = new ArrayList<String>(params.keySet());
            Collections.sort(keys);
            for (String key : keys) {
                LOGGER.info(key + " = " + params.get(key));
                formParams.add(new BasicNameValuePair(key, params.get(key)));
            }
        }
        return formParams;
    }

    private static class TrustAnyTrustManager implements X509TrustManager {

        @Override
        public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        }

        @Override
        public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        }

        @Override
        public X509Certificate[] getAcceptedIssuers() {
            return new X509Certificate[]{};
        }
    }


    /**
     * GET方式提交URL请求，会自动重定向 包涵头信息
     */
    public static String requestGetNew(String url, String responseCharacter, int connectionTimeout, int soTimeout, Map<String, Object> map) {
        HttpResponseWrapper httpResponseWrapper = null;
        String responseStr = "";
        try {
            HttpClient client = null;
            if (url.startsWith("https")) {
                client = createHttpsClient(connectionTimeout, soTimeout);
            } else {
                client = createHttpClient(connectionTimeout, soTimeout);
            }
            HttpGet httpGet = new HttpGet(url);
            if (map != null && map.size() > 0) {
                for (Map.Entry<String, Object> entry : map.entrySet()) {
                    LOGGER.info("key= " + entry.getKey() + " and value= " + entry.getValue());
                    httpGet.setHeader(entry.getKey(), entry.getValue().toString());
                }
            }
            HttpResponse httpResponse = client.execute(httpGet);
            httpResponseWrapper = new HttpResponseWrapper(client, httpResponse);
            responseStr = httpResponseWrapper.getResponseString(responseCharacter);
        } catch (Exception e) {
            responseStr = HTTP_ERROR_PREFIX + e.toString();
            LOGGER.error("requstGet request Exception:", e);
        } finally {
            if (httpResponseWrapper != null) {
                httpResponseWrapper.close();
            }
        }
        return responseStr;
    }

    /**
     * GET方式提交URL请求，会自动重定向 包涵头信息
     */
    public static String requestGetNew(String url, Map<String, Object> map) {
        return requestGetNew(url, CHARACTER_ENCODING, CONNECTION_TIMEOUT, SO_TIMEOUT, map);
    }

    /**
     * psot json方式提交URL请求，会自动重定向 包涵头信息
     */


    public static String requestPostJsonWithHeader(String url, String json, Map<String, Object> map) {
        return requestPostJsonWithHeader(url, json, CHARACTER_ENCODING, CHARACTER_ENCODING, CONNECTION_TIMEOUT, SO_TIMEOUT, map);
    }

    /**
     * psot json方式提交URL请求，会自动重定向 包涵头信息
     */

    public static String requestPostJsonWithHeader(String url, String json, String requestCharacter, String responseCharacter, int connectionTimeout, int soTimeout, Map<String, Object> map) {
        HttpResponseWrapper httpResponseWrapper = null;
        String responseStr = "";
        try {
            HttpClient client = null;
            if (url.startsWith("https")) {
                client = createHttpsClient(connectionTimeout, soTimeout);
            } else {
                client = createHttpClient(connectionTimeout, soTimeout);
            }

            String contentType = "application/json; charset=" + requestCharacter;
            HttpPost httpPost = new HttpPost(url);
            httpPost.setHeader(CONTENT_TYPE, contentType);
            if (map != null && map.size() > 0) {
                for (Map.Entry<String, Object> entry : map.entrySet()) {
                    LOGGER.info("key= " + entry.getKey() + " and value= " + entry.getValue());
                    httpPost.setHeader(entry.getKey(), entry.getValue().toString());
                }
            }
            StringEntity se = new StringEntity(json, requestCharacter);
            httpPost.setEntity(se);
            HttpResponse httpResponse = client.execute(httpPost); //执行POST请求
            httpResponseWrapper = new HttpResponseWrapper(client, httpResponse);

            responseStr = httpResponseWrapper.getResponseString(responseCharacter);
        } catch (Exception e) {
            responseStr = HTTP_ERROR_PREFIX + e.toString();
            LOGGER.error("requestPostForm request Exception:", e);
        } finally {
            if (httpResponseWrapper != null) {
                httpResponseWrapper.close();
            }
        }
        return responseStr;
    }

    /**
     * POST方式提交表单数据，不会自动重定向
     *
     * @param responseCharacter
     */
    public static String requestPostStrData(String url, String data, String contentType, String requestCharacter, String responseCharacter) {
        HttpResponseWrapper httpResponseWrapper = null;
        String responseStr = "";
        try {
            httpResponseWrapper = requestPostData(url, data, contentType, requestCharacter);
            responseStr = httpResponseWrapper.getResponseString(responseCharacter);
        } catch (Exception e) {
            responseStr = HTTP_ERROR_PREFIX + e.toString();
            LOGGER.error("requestPostForm request Exception:", e);
        } finally {
            if (httpResponseWrapper != null) {
                httpResponseWrapper.close();
            }
        }
        return responseStr;
    }

    /**
     * 0 :为流对象  1:为字符串
     *
     * @param url
     * @param json
     * @return
     */
    public static Map<String, Object> requestPostJsonNew(String url, String json, String orderID) {
        HttpResponseWrapper httpResponseWrapper = null;
        Map<String, Object> result = new HashMap<String, Object>();
        String responseStr = "";
        InputStream responseStream = null;
        try {
            httpResponseWrapper = requestPostJsonResponse(url, json, CHARACTER_ENCODING, CONNECTION_TIMEOUT, SO_TIMEOUT);
            HttpEntity entity = httpResponseWrapper.getEntity();
            String contentType = entity.getContentType().getValue();
            if ("application/pdf".equalsIgnoreCase(contentType)) {
                //保存pdf文件
                responseStream = httpResponseWrapper.getResponseStream();
                String filename = System.getProperty("java.io.tmpdir")
                        + System.getProperty("file.separator")
                        + "qrcode_shanghaiDisney_" + orderID + ".pdf";
                File file = new File(filename);
                FileUtils.copyInputStreamToFile(responseStream, file);
                result.put("0", file);
            } else {
                responseStr = httpResponseWrapper.getResponseString(CHARACTER_ENCODING);
                result.put("1", responseStr);
            }
        } catch (Exception e) {
            responseStr = HTTP_ERROR_PREFIX + e.toString();
            result.put("1", responseStr);
            LOGGER.error("requestPostForm request Exception:", e);
        } finally {
            if (httpResponseWrapper != null) {
                httpResponseWrapper.close();
            }
        }
        return result;
    }
}
